const formatTime = (date, symbol1, symbol2) => {
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()
    const hour = date.getHours()
    const minute = date.getMinutes()
    const second = date.getSeconds()

    return [year, month, day].map(formatNumber).join(symbol1 || '/') + ' ' + [hour, minute, second].map(formatNumber).join(symbol2 || ':')
}

const formatTime_1 = (date, symbol1, symbol2) => {
    const month = date.getMonth() + 1
    const day = date.getDate()
    const hour = date.getHours()
    const minute = date.getMinutes()

    return [month, day].map(formatNumber).join(symbol1 || '/') + ' ' + [hour, minute].map(formatNumber).join(symbol2 || ':')
}

const formatTime_3 = (date, symbol1, symbol2) => {
    if (/(iPhone|iPad|iPod|iOS)/i.test(wx.getStorageSync('modelmes')) && typeof(date) != "number") {
        //ios
        date = new Date(date.replace(/-/g, "/"));

    } else {
        date = new Date(date);
    }
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()
    const hour = date.getHours()
    const minute = date.getMinutes()
    const second = date.getSeconds()

    return [year, month, day].map(formatNumber).join(symbol1 || '/') + ' ' + [hour, minute, second].map(formatNumber).join(symbol2 || ':')
}

const formatTime_2 = (date, symbol1) => {
    if (/(iPhone|iPad|iPod|iOS)/i.test(wx.getStorageSync('modelmes')) && typeof(date) != "number") {
        //ios
        date = new Date(date.replace(/-/g, "/"));

    } else {
        date = new Date(date);
    }
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()

    return [year, month, day].map(formatNumber).join(symbol1 || '/');
}

const formatTime_4 = (date, symbol1) => {
    if (/(iPhone|iPad|iPod|iOS)/i.test(wx.getStorageSync('modelmes')) && typeof(date) != "number") {
        //ios
        date = new Date(date.replace(/-/g, "/"));

    } else {
        date = new Date(date);
    }
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()

    return [`${year}年`, `${month}月`, `${day}日`].map(formatNumber).join(symbol1 || '');
}

const formatNumber = n => {
    n = n.toString()
    return n[1] ? n : '0' + n
}
const dealObjectValue = (obj) => {
    let param = {};
    console.log(obj)
    if (obj === null || obj === undefined || obj === "") return param;
    for (let key in obj) {
        if (typeof(obj[key]) === "Object") {
            param[key] = dealObjectValue(obj[key]);
        } else if (obj[key] !== null && obj[key] !== undefined && obj[key] !== "") {
            param[key] = obj[key];
        }
    }
    return param;
}

const testidCard = n => {
    if ((/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(n))) {
        return false;
    } else {
        return true;
    }
}
const formatDate = date => {
    console.log(date)
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()
    return [year, month, day].map(formatNumber).join('/')
}
const IdCard = (UUserCard, num) => {
    if (num == 1) {   //获取出生日期    
        var birth = UUserCard.substring(6, 10) + "-" + UUserCard.substring(10, 12) + "-" + UUserCard.substring(12, 14);
        return birth;
    }
    if (num == 2) {   //获取性别    
        if (parseInt(UUserCard.substr(16, 1)) % 2 == 1) {    //男      
            return "男";
        } else {    //女      
            return "女";
        }
    }
    if (num == 3) {   //获取年龄    
        var myDate = new Date();
        var month = myDate.getMonth() + 1;
        var day = myDate.getDate();
        var age = myDate.getFullYear() - UUserCard.substring(6, 10) - 1;
        if (UUserCard.substring(10, 12) < month || UUserCard.substring(10, 12) == month && UUserCard.substring(12, 14) <= day) {
            age++;
        }
        return age;
    }
}

//进行经纬度转换为距离的计算
function Rad(d) {
    return d * Math.PI / 180.0; //经纬度转换成三角函数中度分表形式。
}

/*
 计算距离，参数分别为第一点的纬度，经度；第二点的纬度，经度
 默认单位km
*/
const getMapDistance = (lat1, lng1, lat2, lng2) => {
    var radLat1 = Rad(lat1);
    var radLat2 = Rad(lat2);
    var a = radLat1 - radLat2;
    var b = Rad(lng1) - Rad(lng2);
    var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) +
        Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
    s = s * 6378.137; // EARTH_RADIUS;
    s = Math.round(s * 10000) / 10000; //输出为公里
    //s=s.toFixed(2);
    return s;
}
const Toleves = (value) => {
    if (value === undefined || value === null) return '';
    if (value == 'A+级（全新)') {
        return 'A+'
    }
    if (value == 'A级（仅试色/余量80%以上）') {
        return 'A'
    }
    if (value == 'B级（余量70%～80%）') {
        return 'B'
    }
    if (value == 'C级（余量60%～70%）') {
        return 'C'
    }
}
const Getleves = (value) => {
    if (value === undefined || value === null) return '';
    if (value == 'A+') {
        return 'A+级（全新)'
    }
    if (value == 'A') {
        return 'A级（仅试色/余量80%以上）'
    }
    if (value == 'B') {
        return 'B级（余量70%～80%）'
    }
    if (value == 'C') {
        return 'C级（余量60%～70%）'
    }
}
module.exports = {
    formatTime: formatTime,
    formatTime_1: formatTime_1,
    formatTime_2: formatTime_2,
    formatTime_3: formatTime_3,
    formatTime_4: formatTime_4,
    dealObjectValue,
    testidCard,
    formatDate,
    IdCard: IdCard,
    getMapDistance,
    Toleves,
    Getleves
}